package org.heart.controller.userInfoController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.heart.common.CommonResult;
import org.heart.entity.dto.FriendRelationship;
import org.heart.entity.dto.UserBaseInfo;
import org.heart.entity.resp.FriendGroupListResp;
import org.heart.service.IFriendRelationshipService;
import org.heart.service.IUserBaseInfoService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static org.heart.common.enums.CommonEnum.COMMON_FIREND_SOURCE;
import static org.heart.common.enums.CommonEnum.COMMON_FRIEND_RELATIONSHHIP;

/**
 * <p>
 * 好友信息关联表 前端控制器
 * </p>
 *
 * @author lml
 * @since 2025-06-29
 */
@RestController
@RequestMapping("/friendRelationship")
public class FriendRelationshipController {

    @Resource
    private IFriendRelationshipService iFriendRelationshipService;

    @Resource
    private IUserBaseInfoService iUserBaseInfoService;

    /**
     * 根据 用户ID 查询好友信息，按照关系类型分组
     */
    @GetMapping("/getFriendList")
    public CommonResult<List<FriendGroupListResp>> getFriendList(@RequestParam("userId") String userId,
                                                                 @RequestParam(value = "nickName",required = false,defaultValue = "") String nickName
                                                                 ) {


        List<FriendGroupListResp> data = iFriendRelationshipService.getFriendGroups(userId,nickName);


        return CommonResult.successResponse(data);
    }

    /**
     * 根据用户Id,添加好有关系信息
     */
    @GetMapping("/addFriend")
    public CommonResult<String> addFriend(@RequestParam(value = "userId",required = false)String userId,
                                          @RequestParam(value = "friendId",required = false) String friendId,
                                          @RequestParam(value = "label",required = false)String label){

        if(StringUtils.isEmpty(userId) || StringUtils.isEmpty(friendId)){
            return CommonResult.errorResponse("请求失败，必要参数【userId】【friendId】为空");
        }

        QueryWrapper<UserBaseInfo> qw2 = new QueryWrapper<>();
        qw2.eq("user_id",friendId);
        List<UserBaseInfo> list2 = iUserBaseInfoService.list(qw2);
        if(ObjectUtils.isEmpty(list2)){
            return CommonResult.errorResponse("该朋友未注册该小程序，添加失败！");
        }

        QueryWrapper<FriendRelationship> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        qw.eq("friend_id",friendId);
        List<FriendRelationship> friend1 = iFriendRelationshipService.list(qw);

        if(!ObjectUtils.isEmpty(friend1)){
            return CommonResult.errorResponse("该朋友已你的在好友列表，添加失败！");
        }

        FriendRelationship friendRelationship = new FriendRelationship();
        friendRelationship.setUserId(userId);
        friendRelationship.setFriendId(friendId);
        friendRelationship.setEstablishTime(new Date());
        if(StringUtils.isEmpty(label)){
            friendRelationship.setRelationType(COMMON_FRIEND_RELATIONSHHIP.getValue());
        }else{
            friendRelationship.setRelationType(label);
        }
        friendRelationship.setSource(COMMON_FIREND_SOURCE.getValue());
        friendRelationship.setMuteNotifications(false);
        friendRelationship.setIsActive(true);

        Boolean save = iFriendRelationshipService.save(friendRelationship);

        if(save){
            return CommonResult.successResponse("操作成功！");
        }else{
            return CommonResult.errorResponse("操作失败！");
        }
    }

}
